package hot;


/**
 * @author way
 * @create 2025-03-11 21:41
 *
 * pre指向反转这一段的末尾
 * cur指向反转这一段后续的下一个节点
 *
 */
public class _92_reverseBetween {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dummy = new ListNode(0, head);
        ListNode p0 = dummy;
        for (int i = 0; i < left - 1; i++) {
            p0 = p0.next;
        }

        ListNode pre = null;
        ListNode cur = p0.next;
        for (int i = 0; i < right - left + 1; i++) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }

        //此时p0还指向要反转的第一个节点
        p0.next.next = cur;
        p0.next = pre;
        return dummy.next;
    }
}
